{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 时间日期，时间序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "from datetime import timedelta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "now = datetime.now() #当前时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2018, 7, 12, 10, 18, 13, 454082)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2018, 7, 12)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.year, now.month, now.day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data1 = datetime(2016,4,24)\n",
    "data2 = datetime(2016,4,20)\n",
    "delta = data1 - data2 #时间差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delta.days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2016, 3, 20, 8, 30)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date = datetime(2016, 3, 20, 8, 30)\n",
    "date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2016-03-20 08:30:00'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'16/03/20 08:30:00'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date.strftime('%y/%m/%d %H:%M:%S')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2016, 3, 20, 8, 30)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.strptime('2016-03-20 08:30', '%Y-%m-%d %H:%M')#时间格式转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dates = [datetime(2016, 2, 1), datetime(2016, 2, 2), datetime(2016, 2, 3), datetime(2016, 2, 4)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 创建时间序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-02-01   -0.654616\n",
       "2016-02-02   -0.398746\n",
       "2016-02-03    1.304034\n",
       "2016-02-04    0.921586\n",
       "dtype: float64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series(np.random.randn(4), index=dates)\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.indexes.datetimes.DatetimeIndex"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(s.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-03-20', '2016-03-21', '2016-03-22', '2016-03-23',\n",
       "               '2016-03-24', '2016-03-25', '2016-03-26', '2016-03-27',\n",
       "               '2016-03-28', '2016-03-29', '2016-03-30'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#生成时间戳的方法\n",
    "pd.date_range('20160320', '20160330')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-03-20', '2016-03-21', '2016-03-22', '2016-03-23',\n",
       "               '2016-03-24', '2016-03-25', '2016-03-26', '2016-03-27',\n",
       "               '2016-03-28', '2016-03-29'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('20160320', periods=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-03-20', '2016-03-21', '2016-03-22', '2016-03-23',\n",
       "               '2016-03-24', '2016-03-25', '2016-03-26', '2016-03-27',\n",
       "               '2016-03-28', '2016-03-29', '2016-03-30'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('20160320 16:32:22', '20160330', normalize=True)#正则化去掉时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-03-31', '2016-04-30', '2016-05-31', '2016-06-30',\n",
       "               '2016-07-31', '2016-08-31', '2016-09-30', '2016-10-31',\n",
       "               '2016-11-30', '2016-12-31'],\n",
       "              dtype='datetime64[ns]', freq='M')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#时间频率,月份，周，几个小时\n",
    "pd.date_range(start='20160320', periods=10, freq='M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-03-20', '2016-03-27', '2016-04-03', '2016-04-10',\n",
       "               '2016-04-17', '2016-04-24', '2016-05-01', '2016-05-08',\n",
       "               '2016-05-15', '2016-05-22'],\n",
       "              dtype='datetime64[ns]', freq='W-SUN')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(start='20160320', periods=10, freq='W')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-03-20 00:00:00', '2016-03-20 04:00:00',\n",
       "               '2016-03-20 08:00:00', '2016-03-20 12:00:00',\n",
       "               '2016-03-20 16:00:00', '2016-03-20 20:00:00',\n",
       "               '2016-03-21 00:00:00', '2016-03-21 04:00:00',\n",
       "               '2016-03-21 08:00:00', '2016-03-21 12:00:00'],\n",
       "              dtype='datetime64[ns]', freq='4H')"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(start='20160320', periods=10, freq='4H')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "##时期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016', 'A-DEC')"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = pd.Period(2016)\n",
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "p = pd.Period(2016, freq='M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-01', 'M')"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2018-01', '2018-02', '2018-03', '2018-04', '2018-05', '2018-06',\n",
       "             '2018-07', '2018-08', '2018-09', '2018-10'],\n",
       "            dtype='period[M]', freq='M')"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#时期的序列\n",
    "pd.period_range('2018-01', periods=10, freq='M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2016Q1', '2016Q2', '2016Q3', '2016Q4', '2017Q1', '2017Q2',\n",
       "             '2017Q3', '2017Q4', '2018Q1', '2018Q2'],\n",
       "            dtype='period[Q-DEC]', freq='Q-DEC')"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#按季度分时间序列\n",
    "pd.period_range('2016Q1', periods=10, freq='Q')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016', 'A-MAR')"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#改变频率\n",
    "p.asfreq('A-MAR')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Period('2015-11', 'M'), Period('2016-01', 'M'))"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = pd.Period('2016Q4', 'Q-JAN')# 以季节分时间，以每年1月份为结束\n",
    "p.asfreq('M', how='start'), p.asfreq('M', how='end')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-01-29 16:19', 'T')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取该季度倒数第二个工作日下午4.20,换成工作日-分钟数\n",
    "(p.asfreq('B') - 1).asfreq('T') + 16 * 60 + 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "##timestamp和period的相互转化\n",
    "s = pd.Series(np.random.randn(5), index=pd.date_range('2016-04-01', periods=5, freq='M'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-04-30    0.747359\n",
       "2016-05-31    0.115710\n",
       "2016-06-30    0.316145\n",
       "2016-07-31    0.166059\n",
       "2016-08-31   -1.268916\n",
       "Freq: M, dtype: float64"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-04    0.747359\n",
       "2016-05    0.115710\n",
       "2016-06    0.316145\n",
       "2016-07    0.166059\n",
       "2016-08   -1.268916\n",
       "Freq: M, dtype: float64"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.to_period()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "####重采样resample，一开始的数据是以每分钟为单位，需要获取每五分钟的数据\n",
    "ts = pd.Series(np.random.randint(0, 50, 60), index=pd.date_range('2016-4-25 09:30', periods=60, freq='T'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-04-25 09:35:00    138\n",
       "2016-04-25 09:40:00    163\n",
       "2016-04-25 09:45:00    152\n",
       "2016-04-25 09:50:00    128\n",
       "2016-04-25 09:55:00    128\n",
       "2016-04-25 10:00:00    152\n",
       "2016-04-25 10:05:00     88\n",
       "2016-04-25 10:10:00     59\n",
       "2016-04-25 10:15:00    172\n",
       "2016-04-25 10:20:00    139\n",
       "2016-04-25 10:25:00     88\n",
       "2016-04-25 10:30:00    160\n",
       "Freq: 5T, dtype: int32"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('5min', label='right').sum()# index为区间右端"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-04-25 09:30:00</th>\n",
       "      <td>14</td>\n",
       "      <td>42</td>\n",
       "      <td>14</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 09:35:00</th>\n",
       "      <td>44</td>\n",
       "      <td>44</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 09:40:00</th>\n",
       "      <td>21</td>\n",
       "      <td>48</td>\n",
       "      <td>9</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 09:45:00</th>\n",
       "      <td>27</td>\n",
       "      <td>38</td>\n",
       "      <td>13</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 09:50:00</th>\n",
       "      <td>38</td>\n",
       "      <td>42</td>\n",
       "      <td>10</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 09:55:00</th>\n",
       "      <td>38</td>\n",
       "      <td>44</td>\n",
       "      <td>10</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 10:00:00</th>\n",
       "      <td>15</td>\n",
       "      <td>44</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 10:05:00</th>\n",
       "      <td>29</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 10:10:00</th>\n",
       "      <td>31</td>\n",
       "      <td>41</td>\n",
       "      <td>31</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 10:15:00</th>\n",
       "      <td>13</td>\n",
       "      <td>45</td>\n",
       "      <td>10</td>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 10:20:00</th>\n",
       "      <td>6</td>\n",
       "      <td>35</td>\n",
       "      <td>6</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 10:25:00</th>\n",
       "      <td>45</td>\n",
       "      <td>45</td>\n",
       "      <td>20</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     open  high  low  close\n",
       "2016-04-25 09:30:00    14    42   14     21\n",
       "2016-04-25 09:35:00    44    44   22     22\n",
       "2016-04-25 09:40:00    21    48    9     38\n",
       "2016-04-25 09:45:00    27    38   13     13\n",
       "2016-04-25 09:50:00    38    42   10     17\n",
       "2016-04-25 09:55:00    38    44   10     20\n",
       "2016-04-25 10:00:00    15    44    6      6\n",
       "2016-04-25 10:05:00    29    29    0     16\n",
       "2016-04-25 10:10:00    31    41   31     31\n",
       "2016-04-25 10:15:00    13    45   10     36\n",
       "2016-04-25 10:20:00     6    35    6     32\n",
       "2016-04-25 10:25:00    45    45   20     35"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对于金融数据，股价的开盘价，最高价，最低价，收盘价\n",
    "ts.resample('5min').ohlc()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-03-01    47\n",
       "2016-03-02     9\n",
       "2016-03-03    25\n",
       "2016-03-04    33\n",
       "2016-03-05    36\n",
       "2016-03-06    46\n",
       "2016-03-07    34\n",
       "2016-03-08    15\n",
       "2016-03-09    17\n",
       "2016-03-10    26\n",
       "2016-03-11    38\n",
       "2016-03-12     8\n",
       "2016-03-13    46\n",
       "2016-03-14    39\n",
       "2016-03-15    44\n",
       "2016-03-16    25\n",
       "2016-03-17    42\n",
       "2016-03-18    44\n",
       "2016-03-19    31\n",
       "2016-03-20     5\n",
       "2016-03-21    49\n",
       "2016-03-22    11\n",
       "2016-03-23    10\n",
       "2016-03-24    36\n",
       "2016-03-25    40\n",
       "2016-03-26     9\n",
       "2016-03-27    19\n",
       "2016-03-28    46\n",
       "2016-03-29    42\n",
       "2016-03-30    40\n",
       "              ..\n",
       "2016-05-10    46\n",
       "2016-05-11    24\n",
       "2016-05-12    43\n",
       "2016-05-13     7\n",
       "2016-05-14    41\n",
       "2016-05-15    23\n",
       "2016-05-16    41\n",
       "2016-05-17    31\n",
       "2016-05-18    34\n",
       "2016-05-19    14\n",
       "2016-05-20     2\n",
       "2016-05-21    20\n",
       "2016-05-22    30\n",
       "2016-05-23     0\n",
       "2016-05-24    25\n",
       "2016-05-25     7\n",
       "2016-05-26    40\n",
       "2016-05-27     1\n",
       "2016-05-28    42\n",
       "2016-05-29    18\n",
       "2016-05-30    16\n",
       "2016-05-31    42\n",
       "2016-06-01     6\n",
       "2016-06-02    47\n",
       "2016-06-03     6\n",
       "2016-06-04    20\n",
       "2016-06-05    18\n",
       "2016-06-06    41\n",
       "2016-06-07     1\n",
       "2016-06-08    25\n",
       "Freq: D, Length: 100, dtype: int32"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##通过groupby重采样\n",
    "ts = pd.Series(np.random.randint(0, 50, 100), index=pd.date_range('2016-03-01', periods=100, freq='D'))\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3    932\n",
       "4    602\n",
       "5    737\n",
       "6    164\n",
       "dtype: int32"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.groupby(lambda x: x.month).sum()#按月份重采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-03    932\n",
       "2016-04    602\n",
       "2016-05    737\n",
       "2016-06    164\n",
       "Freq: M, dtype: int32"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.groupby(ts.index.to_period('M')).sum()#转化成按月的时期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df = pd.DataFrame(np.random.randint(1, 50, 2), index=pd.date_range('2016-4-22', periods=2, freq='W-FRI'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-04-22</th>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-29</th>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             0\n",
       "2016-04-22  31\n",
       "2016-04-29  26"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-04-22</th>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-23</th>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-24</th>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25</th>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-26</th>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-27</th>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-28</th>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-29</th>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             0\n",
       "2016-04-22  31\n",
       "2016-04-23  31\n",
       "2016-04-24  31\n",
       "2016-04-25  31\n",
       "2016-04-26  31\n",
       "2016-04-27  31\n",
       "2016-04-28  31\n",
       "2016-04-29  26"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.resample('D').ffill()# 以天采样后，缺失的值以上一个值填补"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
